﻿// Jinx AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.IO;
using Jinx.Applications;
using Jinx.Communications.Applications;
using Jinx.Communications.Applications.Services;
using Jinx.Communications.Content;
using Jinx.Communications.Hosting;
using Jinx.Communications.Views;
using Jinx.Utility;

namespace Jinx.Communications
{
    using VDirs = Constants.VirtualDirectories;
    using VConst = Constants.Views;

    public class AppServer : IDisposable
    {
        public AppServer(string Host, short Port, string VirtualDirectory,
                         IFactory<ApplicationContext> ApplicationContextFactory,
                         IContentFactory ContentFactory)
        {
            // Prep the view servers

            var MainTransformStream = ContentFactory.GetContentStream(VConst.MAIN_VIEW_TEMPLATE);
            var SecTransformStream = ContentFactory.GetContentStream(VConst.VIEW_LIBRARY_TEMPLATE);

            if (MainTransformStream == null)
                throw new Exception("Could not load a file needed internally: " + VConst.MAIN_VIEW_TEMPLATE);
            if (SecTransformStream == null)
                throw new Exception("Could not load a file needed internally: " + VConst.VIEW_LIBRARY_TEMPLATE);

            Func<string, Stream> cf = s => ContentFactory.GetContentStream(s);

            var MainViewServer = new ViewServer(MainTransformStream, "application/xhtml+xml", cf);
            var ViewLibraryServer = new ViewServer(SecTransformStream, "text/javascript", cf);

            // Application scaffolding

            ApplicationContextManager = new ApplicationContextManager(ApplicationContextFactory);
            var NewAppServer = new ApplicationCreationServer(ApplicationContextManager, VirtualDirectory, MainViewServer);

            // Communications

            var Servers = new Dictionary<string, IServer>
                          {
                              {VDirs.APPLICATION_CREATION_SERVER, NewAppServer},
                              {VDirs.POLLING_SERVER, new PollingServer(ApplicationContextManager)},
                              {VDirs.ACTION_SERVER, new ServiceHost(ApplicationContextManager)},
                              {VDirs.VIEW_SERVER, ViewLibraryServer},
                              {VDirs.CONTENT_SERVER, new ContentServer(ContentFactory.GetContentStream)}
                          };

            ServerHost = new ServerHost(Host, Port, VirtualDirectory, Servers);
            EntryPoint = string.Format("http://{0}:{1}/{2}", Host, Port, VirtualDirectory);
        }

        public string EntryPoint { get; private set; }

        public ServerHost ServerHost { get; private set; }

        public ApplicationContextManager ApplicationContextManager { get; private set; }

        #region IDisposable Members

        public void Dispose()
        {
            ServerHost.Dispose();
            ApplicationContextManager.Dispose();
        }

        #endregion
    }
}